﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using UtilityExtensions;
using BOL;
using BOL.UI;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;

namespace UILGestion.Admin.Proyecto
{
    public partial class ProyectoCuentaEditGrid : System.Web.UI.UserControl
    {
        public ProyectoCuentaEditGrid()
        {
            bol = new ProyectoBL();
    }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {

                idProyecto.Value = (string)Request.QueryString["id"];
                FormatForm();
            }
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            
            
        }
       
        
        private void FormatForm()
        {
                BindDataItem();
                setFooterRow();
        }

        ProyectoBL bol;


        protected void AddItem(object sender, EventArgs e)
        {
            try
            {
                DropDownList cuentaDDL =
                    ((DropDownList)itemGridView.FooterRow.FindControl("cuentaDDL"));

                CheckBox bitActivoCHK =
                    ((CheckBox)itemGridView.FooterRow.FindControl("bitActivoCHK"));

                var cuentaproyecto= new CuentaProyecto();

                using (dbGestionDataContext _db = new dbGestionDataContext(DbUtil.strConnString))
                {
                    cuentaproyecto.SetAdd(_db, cuentaDDL.SelectedValue.ToInt(), idProyecto.Value.ToInt(), bitActivoCHK.Checked);
                    _db.SubmitChanges();
                }
                
                BindDataItem();
                setFooterRow();
                GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void UpdateItem(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);
                int idinternal = int.Parse(gv.DataKeys[e.RowIndex].Values[0].ToString());

               
                //DropDownList cuentaDDL =
                //   ((DropDownList)gv.Rows[e.RowIndex].FindControl("cuentaDDL"));

                CheckBox bitActivoCHK =
                    ((CheckBox)gv.Rows[e.RowIndex].FindControl("bitActivoCHK"));


                using (dbGestionDataContext _db = new dbGestionDataContext(DbUtil.strConnString))
                {
                    CuentaProyecto cuentaproyecto = _db.CuentaProyectos.Single(p => p.idInternal == idinternal);

                    cuentaproyecto.SetUpdate(bitActivoCHK.Checked);

                    _db.SubmitChanges();
                }
                

                gv.EditIndex = -1;

                BindDataItem();
                setFooterRow();
                 GridUpdatePanel.Update();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void EditItem(object sender, GridViewEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = e.NewEditIndex;

                BindDataItem();
                itemGridView.FooterRow.Visible = false;
                GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void CancelItem(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = -1;
                BindDataItem();
                GridUpdatePanel.Update();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void DeleteItem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());

                using (dbGestionDataContext _db = new dbGestionDataContext())
                {
                    var drw = _db.CuentaProyectos.Single(p => p.idInternal == idInternal);
                   

                    _db.CuentaProyectos.DeleteOnSubmit(drw);
                    _db.SubmitChanges();
                }

                
                BindDataItem();
                setFooterRow();
                GridUpdatePanel.Update();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        private void resetEmpty()
        {
            if (itemGridView.Rows.Count == 0)
            {
                List<BOL.ProyectoCuentaInfo> itemList0 = new List<BOL.ProyectoCuentaInfo>();
                BOL.ProyectoCuentaInfo itemcero = new BOL.ProyectoCuentaInfo();
                itemcero.idInternal = 0;
                itemList0.Add(itemcero);
                itemGridView.DataSource = itemList0;
                itemGridView.DataBind();
                itemGridView.Rows[0].Visible = false;
            }

        }

        public void BindDataItem()
        {



            itemGridView.DataSource = bol.GetProyectoCuenta(idProyecto.Value.ToInt());
            itemGridView.DataBind();
            resetEmpty();

        }

        private void setFooterRow()
        {

            DropDownList cuentaDDL =
                    ((DropDownList)itemGridView.FooterRow.FindControl("cuentaDDL"));


            var listaOrigen = DbUtil.Db.CuentaProyectos.Select(p => p.idCuentaBanco);

            var concepto = from a in DbUtil.Db.CuentaBancos
                           where !listaOrigen.Contains(a.idInternal)
                                   
                           select a;

            cuentaDDL.DataValueField = "idInternal";
            cuentaDDL.DataTextField = "descripcion";
            cuentaDDL.DataSource = concepto;
            cuentaDDL.DataBind();



        }

    }

}